<template>
	<div>
		<div class="h2">积分预警列表</div>
		<div id="components-form-demo-advanced-search">
			<a-form class="ant-advanced-search-form home-form" :form="form" @submit="handleSearch">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="名称">
							<a-input autocomplete="off" class="field-right" placeholder="请输入名称搜索"
								v-decorator="[`key`]" />
						</a-form-item>
					</a-col>
					<!-- <a-col :span="8">
						<a-form-item label="部门">
							<a-select :allowClear="true" v-decorator="['departCode']" placeholder="全部" optionFilterProp="children" showSearch>
								<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="年级">
							<a-date-picker
								mode="year"
								placeholder="请选择年级"
								format="YYYY"
								:value="gradeYear"
								:open="yearPickShow"
								@panelChange="handlePanelChange"
								@openChange="handleOpenChange"
								@change="yearChange"
							/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						
					<a-form-item label="课程类型">
						<a-tree-select :allowClear="true" show-search
							v-decorator="['ActivityTypeID']"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							:tree-data="ActivityTypeApplyList" tree-default-expand-all :replace-fields="replaceFields"
							placeholder="请先选择课程类型" @change="selectChange"></a-tree-select>
					</a-form-item>
					</a-col>
					<a-col :span="8" style="display: flex;" v-if="qujianStatus">
						<a-form-item label="分值区间">
							<a-input type="number"  v-decorator="['totalScore1',{ rules: [{ required: true, message: '分值区间不能为空' }] }]" style="width: 155px;" placeholder="" />
						</a-form-item>
						~
						<a-form-item label="">
							<a-input type="number" v-decorator="['totalScore2',{ rules: [{ required: true, message: '分值区间不能为空' }] }]" style="width: 155px;"  placeholder="" />
						</a-form-item>
						</a-col> -->
					<!-- <a-col :span="8">
						<a-form-item label="部门">
							<a-select :allowClear="true" v-decorator="['departCode']" placeholder="全部" optionFilterProp="children" showSearch>
								<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
							</a-select>
						</a-form-item>
					</a-col> -->
					<!-- <a-col :span="8" style="margin-top:4px;">
						<a-button type="primary" html-type="submit" class="btn1">搜索</a-button>
						<a-button :style="{ marginLeft: '8px' }" @click="handleReset" class="btn2">重置</a-button>
					</a-col> -->
					<a-col :span="8" style="margin-top:4px;">
						<a-button-group size="default" class="option">
							<a-button type="primary" @click="handleSearch">搜索</a-button>
							<a-button @click="handleReset">重置</a-button>
							<a-button type="primary" @click="addList">新增</a-button>
						</a-button-group>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div>
			<el-table :data="data" v-loading="loading" border :max-height="tableHeight" highlight-current-row
				style="width: 100%;">
				<el-table-column type="index" label="序号" width="60" fixed="left"></el-table-column>
				<el-table-column prop="Title" label="名称"></el-table-column>
				<el-table-column prop="Type" label="预警间隔">
					<template slot-scope="scope">
						{{scope.row.Type==1 ? '每月' :scope.row.Type==2 ? '每季度' :scope.row.Type==3 ? '每年' :'' }}
					</template>
				</el-table-column>
				<el-table-column prop="SendTime" label="预警日期">
					<template slot-scope="scope" v-if="scope.row.SendTime">
						{{(scope.row.Type==1||scope.row.Type==2) ? scope.row.SendTime.substr(8) :scope.row.SendTime.substr(5) }}
					</template>
				</el-table-column>
				<el-table-column prop="DepartName" label="部门" show-overflow-tooltip>
					<template slot-scope="scope">
						{{ scope.row.DepartCodeName }}
					</template>
				</el-table-column>
				<el-table-column prop="Grade" label="年级" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column prop="Content" label="描述" show-overflow-tooltip></el-table-column> -->
				<el-table-column label="操作" width="180" fixed="right">
					<template slot-scope="scope">
						<oprateBtn title="修改" icon="icon-editor1" color="#1890FF" @tap="modifyList(scope.row,'modify')">
						</oprateBtn>
						<oprateBtn title="删除" icon="icon-delete" color="red" @tap="deleteList(scope.row,1)"></oprateBtn>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="pagination.currentPage" :page-sizes="pagination.pageSizeOptions"
				:page-size="pagination.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="pagination.total"></el-pagination>
		</div>
		<add :text="text" :DepartCodeList="DepartCodeList" :ClassList="ClassList" v-show="addIf" @closeFun="closeFun">
		</add>
	</div>
</template>

<script>
	import Cache from '@/utils/cache';
	import add from './add';
	import Func from '@/utils/func';
	import moment from 'moment';
	import {
		ManageCreditWarningDoGetPageList,
		ManageCreditWarningDoDelete,
		GetDepartList,
		GetClassList,
		DoUnbundTeacher,
		DoEnableTeacher,
		GetActivityTypeList
	} from '@/api/follow';
	const data = [];
	export default {
		components: {
			add
		},
		data() {
			return {
				ActivityTypeApplyList:[],
				replaceFields: {
					children: 'children',
					title: 'title',
					key: 'ID',
					value: 'ID'
				},
				data,
				form: this.$form.createForm(this, {
					name: 'advanced_search'
				}),
				pagination: {
					currentPage: 1,
					pageSize: this.global.pageSize,
					pageSizeOptions: this.global.pageSizeOptions, // 每页中显示的数据
					showTotal: total => `共有 ${total} 条数据` // 分页中显示总的数据
				},
				addIf: false,
				qujianStatus:false,
				text: {},
				visible: false,
				confirmLoading: false,
				itemId: '',
				gradeYear: null,
				yearPickShow: false,
				departmentList: [],
				worklist: [],
				loading: false,
				values: {},
				pages: {
					pageIndex: 1,
					pageSize: 20
				},
				tableHeight: parseFloat(window.innerHeight - 160),
				ClassList: [],
				DepartCodeList: [],
				requestUrl: this.global.requestUrl,
				token: Cache.get('token')
			};
		},
		computed: {},
		created() {
			this.getList();
			 this.getOtherList();
		},
		mounted() {},
		methods: {
			moment,
			exportList() {
				this.form.validateFields((error, values) => {
					let data = {};
					data.key = values.key;
					data.departCode = values.departCode;
					let hrefUrl = this.requestUrl + 'Manage/Teacher/DoExport?userToken=' + encodeURIComponent(this
						.token);
					let hrefStr = '';
					for (let key in data) {
						if (data[key]) {
							hrefStr += key + '=' + data[key] + '&';
						}
					}
					window.open(hrefUrl + '&' + hrefStr, '_blank');
				});
			},
			selectChange(e){
				this.activityTypeID = e
				if(e){
					this.qujianStatus=true
				}else{
					this.qujianStatus=false
				}
				
				this.getList(e);
			},
			handleOpenChange(status) {
				this.yearPickShow = status;
			},
			handlePanelChange(value, mode) {
				this.gradeYear = moment(value).format('YYYY');
				this.yearPickShow = false;
			},
			yearChange(date, dateString) {
				if (!date) {
					this.gradeYear = null;
				}
			},
			handleSearch(e) {
				e.preventDefault();
				this.form.validateFields((error, values) => {
					this.pagination.currentPage = 1;
					this.values = values;
					if (this.gradeYear) {
						this.values.grade = this.gradeYear;
					}
					if(values.ActivityTypeID!=undefined&&values.totalScore1!=undefined&&values.totalScore2!=undefined){
						values.activityScore=values.ActivityTypeID+'|'+values.totalScore1+'_'+values.totalScore2
					}
					this.getList();
				});
			},
			handleReset() {
				this.form.resetFields();
				this.values = {};
				this.gradeYear = null;
				this.getList();
			},
			handleSizeChange(val) {
				this.pagination.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.pagination.currentPage = val;
				this.getList();
			},
			async getList() {
				this.loading = true;
				let data = {};
				if (this.values) {
					data = this.values;
				}
				data.pageIndex = this.pagination.currentPage;
				data.pageSize = this.pagination.pageSize;
				let res = await ManageCreditWarningDoGetPageList(data);
				this.loading = false;
				this.data = res.data.data;
				const pagination = {
					...this.pagination
				};
				pagination.total = res.data.pageInfo.TotalItemCount;
				this.pagination = pagination;
			},
			async getOtherList() {
				let res = await GetClassList();
				this.ClassList = res.data.data;
				let res1 = await GetDepartList();
				this.DepartCodeList = res1.data.data;
			let res2 = await GetActivityTypeList()
				this.ActivityTypeData = res2.data.data
				this.ActivityTypeApplyList= res2.data.data
				this.ActivityTypeApplyList = this.ActivityTypeApplyList.map(item => {
					return {
						...item,
						title: item.Name,
						// disabled: item.IsApply ? false : true
					};
				});
				this.ActivityTypeApplyList = this.toTree(this.ActivityTypeApplyList);
				console.log(this.ActivityTypeApplyList);
				
			},
			toTree(data) {
				let result = [];
				let map = {};
				if (!Array.isArray(data)) {
					return [];
				}
				data.forEach(item => {
					map[item.ID] = item;
				});
				data.forEach(item => {
					let parent = map[item.ParentID];
					if (parent) {
						(parent.children || (parent.children = [])).push(item);
					} else {
						result.push(item);
					}
				});
				return result;
			},
			addList() {
				this.text = {};
				this.addIf = !this.addIf;
			},
			modifyList(text) {
				text = Object.assign(text, {
					time: new Date()
				});
				this.addIf = !this.addIf;
				this.text = text;
			},
			deleteList(text, type) {
				let that = this;
				this.$confirm({
					title: '提示',
					content: type == 1 ? `您确定删除该问卷吗？` : type == 2 ? `您确定要解绑该教师的微信吗？` : `您确定要修改该老师的状态吗？`,
					okText: '确认',
					cancelText: '取消',
					async onOk() {
						let data = {};
						data.id = text.ID;
						let res = type == 1 ? await ManageCreditWarningDoDelete(data) : type == 2 ?
							await DoUnbundTeacher(data) : await DoEnableTeacher(data);
						if (res.data.code == 0) {
							that.getList();
							that.$message.success(res.data.msg);
						} else {
							that.$message.error(res.data.msg);
						}
					}
				});
			},
			seeList(text, name) {
				this.$router.push({
					name: name,
					query: {
						ID: text.ID,
						Name: text.Name,
						UserNo: text.UserNo
					}
				});
			},
			closeFun() {
				this.addIf = false;
				this.getList();
			}
		}
	};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		display: flex;
		margin-bottom: 10px !important;
	}

	/deep/.ant-form-item-label {
		width: 80px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}

	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}

	/deep/.ant-calendar-picker {
		width: 100%;
	}
</style>
